package com.infra.micro.quartz.domain.service.impl;

import com.infra.micro.quartz.domain.entity.QuartzJobLogPO;
import com.infra.micro.quartz.domain.service.JobAlarmService;
import com.infra.micro.quartz.model.vo.QuartzJobVO;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

@Component
public class JobAlarm implements ApplicationContextAware, InitializingBean {

    private ApplicationContext applicationContext;
    //告警BEAN对象MAP
    private Map<String, JobAlarmService> serviceBeanMap;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() {
        serviceBeanMap = applicationContext.getBeansOfType(JobAlarmService.class);
    }

    public void send(QuartzJobVO jobDetail, QuartzJobLogPO logDetail) throws Exception {
        if (CollectionUtils.isEmpty(serviceBeanMap)) {
            return;
        }

        for (Map.Entry<String, JobAlarmService> item : serviceBeanMap.entrySet()) {
            item.getValue().send(jobDetail, logDetail);
        }
    }

}

